package com.mall.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.mapper.PmsProductCategoryAttributeRelationMapper;
import com.mall.mapper.ProductCategoryMapper;
import com.mall.model.domain.PmsProductCategory;
import com.mall.model.domain.PmsProductCategoryAttributeRelation;
import com.mall.model.dto.ProductCategoryDto;
import com.mall.model.vo.PageResult;
import com.mall.model.vo.ProductCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;
    /**
     * 添加商品分类
     * @param productCategoryDto
     */
    public void save(ProductCategoryDto productCategoryDto) {
        PmsProductCategory pmsProductCategory=new PmsProductCategory();
        BeanUtils.copyProperties(productCategoryDto,pmsProductCategory);
        //保存到商品分类表
        productCategoryMapper.insert(pmsProductCategory);
        //商品分类表还有级别属性(需要判断)
        Long parentId = pmsProductCategory.getParentId();
        //获取pmsProductCategory的id
        Long categoryId = pmsProductCategory.getId();
        if(parentId==categoryId){
            pmsProductCategory.setLevel(1);
            PmsProductCategory category = productCategoryMapper.selectById(categoryId);
            pmsProductCategory.setProductCount(category.getProductCount());
        }else {
            pmsProductCategory.setLevel(0);
            pmsProductCategory.setProductCount(0);
        }
        productCategoryMapper.updateById(pmsProductCategory);
        //创建商品关系表对象
        PmsProductCategoryAttributeRelation ppca=new PmsProductCategoryAttributeRelation();
        ppca.setProductCategoryId(categoryId);
        //再保存商品关系表
        List<Integer> productAttributeIdList = productCategoryDto.getProductAttributeIdList();
        for (Integer integer : productAttributeIdList) {
            Long productAttributeId = Long.valueOf(integer.toString());
           ppca.setProductAttributeId(productAttributeId);
            pmsProductCategoryAttributeRelationMapper.insert(ppca);
        }

    }

    /**
     * 删除商品分类
     * @param id
     */
    public void deleteById(Integer id) {
        //删除分类表
        productCategoryMapper.deleteById(id);
        //删除分类关系表数据
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation>lqw=new LambdaQueryWrapper<>();
        lqw.in(PmsProductCategoryAttributeRelation::getProductCategoryId,id);
        pmsProductCategoryAttributeRelationMapper.delete(lqw);
    }

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    public List<ProductCategoryVo> selectList() {
        LambdaQueryWrapper<PmsProductCategory>lqw=new LambdaQueryWrapper<>();
        //一级分类
        lqw.eq(PmsProductCategory::getLevel,0);
        List<PmsProductCategory> list = productCategoryMapper.selectList(lqw);
        //二级分类
        List<ProductCategoryVo>voList=new ArrayList<>();
        for (PmsProductCategory pmsProductCategory : list) {
            ProductCategoryVo vo=new ProductCategoryVo();
            BeanUtils.copyProperties(pmsProductCategory,vo);
            LambdaQueryWrapper<PmsProductCategory>wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(PmsProductCategory::getParentId,pmsProductCategory.getId());
            List<PmsProductCategory> list2 = productCategoryMapper.selectList(wrapper);
            vo.setChildren(list2);
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 分页查询商品分类
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResult selectByPage(Integer parentId, Integer pageNum, Integer pageSize) {
        Page page=new Page(pageNum,pageSize);
        LambdaQueryWrapper<PmsProductCategory>lqw=new LambdaQueryWrapper<>();
        lqw.eq(parentId!=null,PmsProductCategory::getParentId,parentId);
        lqw.orderByDesc(PmsProductCategory::getSort);
        Page pageInfo = productCategoryMapper.selectPage(page, lqw);

        List records = pageInfo.getRecords();
        long total = pageInfo.getTotal();
        long pages = pageInfo.getPages();
        long size = pageInfo.getSize();
        return  new PageResult((int)pages,(int)size,(int)total,records);
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     */
    public void updateNavStatus(List<Integer> ids, Integer navStatus) {
        LambdaQueryWrapper<PmsProductCategory>lqw=new LambdaQueryWrapper<>();
        lqw.in(ids!=null,PmsProductCategory::getId,ids);
        List<PmsProductCategory> list = productCategoryMapper.selectList(lqw);
        for (PmsProductCategory pmsProductCategory : list) {
            pmsProductCategory.setNavStatus(navStatus);
            productCategoryMapper.updateById(pmsProductCategory);
        }
    }

    /**
     * 修改显示状态
     * @param ids
     * @param showStatus
     */
    public void updateShowStatus(List<Integer> ids, Integer showStatus) {
        LambdaQueryWrapper<PmsProductCategory>wrapper=new LambdaQueryWrapper<>();
        wrapper.in(ids!=null,PmsProductCategory::getId,ids);
        List<PmsProductCategory> list = productCategoryMapper.selectList(wrapper);
        for (PmsProductCategory pmsProductCategory : list) {
            pmsProductCategory.setNavStatus(showStatus);
            productCategoryMapper.updateById(pmsProductCategory);
        }
    }

    /**
     * 修改商品分类
     * @param id
     * @param productCategoryDto
     */
    public void updateProduct(Integer id, ProductCategoryDto productCategoryDto) {
        //更改商品分类表
      long id1=Long.valueOf(id.toString());
        productCategoryDto.setId(id1);
        productCategoryMapper.updateById(productCategoryDto);
        //更改商品-属性中间表(先根据分类id删除原来的数据,再将新的数据重新添加保存)
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation>lqw=new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategoryAttributeRelation::getProductCategoryId,id1);
        pmsProductCategoryAttributeRelationMapper.delete(lqw);
        //将新的数据保存
        List<Integer> productAttributeIdList = productCategoryDto.getProductAttributeIdList();
        //将integer类型的集合转化成long类型的
        List<Long> attributeIdList = productAttributeIdList.stream().map(item -> {
            return Long.valueOf(item.toString());
        }).collect(Collectors.toList());
        PmsProductCategoryAttributeRelation ppca=new PmsProductCategoryAttributeRelation();
        ppca.setProductCategoryId(id1);
        for (Long aLong : attributeIdList) {
            ppca.setProductAttributeId(aLong);
            pmsProductCategoryAttributeRelationMapper.insert(ppca);
        }
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    public PmsProductCategory selectById(Integer id) {
        PmsProductCategory pmsProductCategory = productCategoryMapper.selectById(id);
        return pmsProductCategory;
    }
}
